<style>div.gridbox_xp table.obj td {border-bottom: none;border-right:none;}</style>

<script>
// Tab State Class definition
// == BEGIN ==
function TabStateClass() {
}

// initialize default date range (2 weeks)
TabStateClass.prototype.params = {"dateBegin":"<?=date('Y-m-d',strtotime('-2 weeks',time()))?>","dateEnd":"<?=date('Y-m-d')?>","selectedLabTests":0};

TabStateClass.prototype.setParam = function(params) {
	if (typeof params == "object") {
		for (var key in params) {
			eval('this.params.'+key+' = params[key];');
		}
	}
}
TabStateClass.prototype.redrawTab = function() {
	redrawLabTestsView();
}
TabStateClass.prototype.getParam = function(index) {
	var ret = null;
	if (typeof this.params == "object") {
		for (var key in this.params) {
			if (key == index) {
				ret = this.params[key];
				break;
			}
		}
	}
	return ret;
}
TabStateClass.prototype.getAllParams = function() {
	return this.params;
}
// instantiate global object
TabState = new TabStateClass();
// == END ==


function redrawTabGrid(params) {
}

var dhxWin = new dhtmlXWindows();
dhxWin.setImagePath("<?=$this->baseUrl?>/img/");
dhxWin.setSkin("clear_silver");

function openSelectLabTestsWindow() {
	winSelectLabTests = dhxWin.createWindow('windowSelectLabTests',60,10,350,240);
	winSelectLabTests.setText('<?=('Select Lab Tests')?>');
	var url = '<?=$this->baseUrl?>/lab-results.raw/select-lab-tests';
	winSelectLabTests.attachURL(url,true);
	winSelectLabTests.setModal(true);
	winSelectLabTests.centerOnScreen();
}

function closeSelectLabTestsWindow() {
	winSelectLabTests.close();
}

function openWorksheetWindow() {
	winWorksheet = dhxWin.createWindow('windowSelectLabTests',60,10,400,350);
	winWorksheet.setText('<?=('Select Lab Tests for Worksheet')?>');
	var url = '<?=$this->baseUrl?>/lab-results.raw/worksheet';
	winWorksheet.attachURL(url,true);
	winWorksheet.setModal(true);
	winWorksheet.centerOnScreen();
}

function closeWorksheetWindow() {
	winWorksheet.close();
}

function openDateRangeWindow() {
	winDateRange = dhxWin.createWindow('windowDateRange',60,10,175,150);
	winDateRange.setText('<?=('Select a Date Range')?>');
	var url = '<?=$this->baseUrl?>/lab-results.raw/date-range';
	winDateRange.attachURL(url,true);
	winDateRange.setModal(true);
	winDateRange.centerOnScreen();
}

function closeDateRangeWindow() {
	winDateRange.close();
}

function backLabResults() {
	changeDateRange(false);
	reloadLabResultsGrid();
}

function forwardLabResults() {
	changeDateRange(true);
	reloadLabResultsGrid();
}

function changeDateRange(advance) {
	if (typeof advance == 'undefined') {
		advance = true;
	}
	var dateBegin = TabState.getParam("dateBegin");
	var dateEnd = TabState.getParam("dateEnd");
	var oDateBegin = dojo.date.stamp.fromISOString(dateBegin);
	var oDateEnd = dojo.date.stamp.fromISOString(dateEnd);
	var dayDiff = dojo.date.difference(oDateBegin,oDateEnd,defaultDateInterval);
	if (advance) {
		oTmpDate = dojo.date.add(oDateEnd,defaultDateInterval,dayDiff);
	}
	else {
		oTmpDate = dojo.date.add(oDateBegin,defaultDateInterval,-dayDiff);
	}
	var day = oTmpDate.getDate();
	if (day < 10) {
		day = '0' + day;
	}
	var month = oTmpDate.getMonth() + 1;
	if (month < 10) {
		month = '0' + month;
	}
	var yr = oTmpDate.getFullYear();
	var tmpDate = yr + '-' + month + '-' + day;

	if (advance) {
		dateBegin = dateEnd;
		dateEnd = tmpDate;
	}
	else {
		dateEnd = dateBegin;
		dateBegin = tmpDate;
	}
	TabState.setParam({"dateBegin":dateBegin,"dateEnd":dateEnd});
}

</script>

<div id="labResultsLayout" style="position:relative;width:100%;height:480px;"></div>
<div id="labResultsLeftLayout" style="position:relative;width:100%;height:480px;"></div>

<div id="labResultsRightContainer" style="float:left;width:100%;">
	<div style="padding-left:5px;height:30px;padding-top:4px; margin:0;background-color:#ece9d8;">
		<div style="float:left;">
			<input type="button" id="backButton" value="Back" onClick="backLabResults()">&nbsp; <strong>Showing:</strong> <label id="labResultLabel">All Tests</label>
		</div>
		<div style="float:right;">
			<input type="button" id="forwardButton" value="Forward" onClick="forwardLabResults()">
		</div>
	</div>
	<div id="labTestsView" style="position:relative;width:100%;"></div>
</div>

<script>
var defaultDateInterval = 'day';
var defaultDate = 'today';
var selectedDate = defaultDate;

// Main Layout
var labLayout = new dhtmlXLayoutObject("labResultsLayout", "2U");
labLayout.setImagePath("<?=$this->baseUrl?>/img/");
currentWidth = labLayout.cells("a").getWidth() / 2;
labLayout.cells("a").setWidth(currentWidth);
labLayout.cells("a").hideHeader();
labLayout.cells("a").attachObject("labResultsLeftLayout");
labLayout.cells("a").fixSize(true,false); 
labLayout.cells("b").hideHeader();
labLayout.cells("b").attachObject("labResultsRightContainer");
labLayout.cells("b").fixSize(true,false); 
labLayout.setEffect('collapse',true);
labLayout.setEffect("resize",true);

//Left Layout
var labResultsLeftLayout = new dhtmlXLayoutObject("labResultsLeftLayout", "3E");
labResultsLeftLayout.setImagePath("<?=$this->baseUrl?>/img/");
labResultsLeftLayout.cells("a").hideHeader();
labResultsLeftLayout.cells("a").setHeight("150");
labResultsLeftLayout.cells("a").fixSize(false,true); 
var lrepGrid = labResultsLeftLayout.cells("a").attachGrid();
labResultsLeftLayout.cells("b").hideHeader();
labResultsLeftLayout.cells("b").setHeight("250");
labResultsLeftLayout.cells("b").fixSize(false,true); 
var drGrid = labResultsLeftLayout.cells("b").attachGrid();
labResultsLeftLayout.cells("c").hideHeader();
labResultsLeftLayout.cells("c").fixSize(false,true); 
var loGrid = labResultsLeftLayout.cells("c").attachGrid();
labResultsLeftLayout.setEffect('collapse',true);
labResultsLeftLayout.setEffect("resize", true);
</script>

<div>
	<div style="width:20%;float:left;"></div>
</div>

<script>
//lrepGrid = new dhtmlXGridObject('labReportsContainer');
lrepGrid.setImagePath("<?=$this->baseUrl?>/img/");
lrepGrid.setHeader('<?=__('Reports')?>');
lrepGrid.setInitWidths("*");
lrepGrid.setColTypes("ro");
lrepGrid.setSkin("xp");
lrepGrid.attachEvent('onRowSelect',onLabReportsRowSelected);
lrepGrid.init();
lrepGrid.load('<?=$this->baseUrl?>/lab-results.raw/reports.json',"json");

function onLabReportsRowSelected(rowId,cellIndex) {
	patientId = mainController.getActivePatient();
	if (patientId <= 0) {
		alert('<?=__('No patient selected.')?>');
		return;
	}
	switch (rowId) {
		case 'tests_by_date':
			TabState.setParam({"selectedLabTests":0});
			redrawLabTestsView();
			break;
		case 'selected_tests_by_date':
			openSelectLabTestsWindow();
			break;
		case 'worksheet':
			openWorksheetWindow();
			break;
		case 'cumulative':
		case 'microbiology':
		case 'anatomic_pathology':
		case 'blood_bank':
		case 'lab_status':
			redrawLabTestsView('report-tests-view');
			break;
	}
}
redrawLabTestsView();

function reloadLabResultsGrid() {
	if (!window.resultsGrid) {
		return;
	}
	resultsGrid.clearAll();
	resultsGrid.load('<?=$this->baseUrl?>/lab-results.raw/results.json?patientId='+mainController.getActivePatient()+'&dateBegin='+TabState.getParam('dateBegin')+'&dateEnd='+TabState.getParam('dateEnd')+'&selectedLabTests='+TabState.getParam('selectedLabTests'),onPostLoad,"json");
	setLabResultLabel();
}

function redrawLabTestsView(view) {
	if (!view) {
		view = 'lab-tests-view';
	}
	if (view == 'lab-tests-view' && TabState.getParam("labTestView") == view) { // cache view
		reloadLabResultsGrid();
		return;
	}
	TabState.setParam({"labTestView":view});
	dojo.xhrGet( {
		url: "<?=$this->baseUrl?>/lab-results.raw/"+view,
		handleAs: "text",
		load: function(data, ioArgs) {
			dojo.setInnerHTML(dojo.byId('labTestsView'),data);
			return data;
		},
		error: function(response, ioArgs) {
			console.error("HTTP status code: ", ioArgs.xhr.status);
			return response;
		}
	});
}

drGrid.setImagePath("<?=$this->baseUrl?>/img/");
drGrid.setHeader('<?=__('Date Range')?>');
drGrid.setInitWidths("*");
drGrid.setColTypes("ro");
drGrid.setSkin("xp");
drGrid.attachEvent('onRowSelect',onDateRangeRowSelected);
drGrid.init();
drGrid.load('<?=$this->baseUrl?>/lab-results.raw/date-range.json',"json");

function onDateRangeRowSelected(rowId,cellIndex) {
	patientId = mainController.getActivePatient();
	if (patientId <= 0) {
		alert('<?=__('No patient selected.')?>');
		return;
	}

	// enable forward and backward buttons
	dojo.byId("forwardButton").disabled = false;
	dojo.byId("backButton").disabled = false;

	switch (rowId) {
		case 'date_range':
			defaultDateInterval = "day";
			openDateRangeWindow();
			break;
		case 'today':
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d')?>"});
			defaultDateInterval = "day";
			break;
		case 'one_week':
			defaultDateInterval = "week";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-1 week',time()))?>"});
			break;
		case 'two_weeks':
			defaultDateInterval = "week";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-2 weeks',time()))?>"});
			break;
		case 'one_month':
			defaultDateInterval = "month";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-1 month',time()))?>"});
			break;
		case 'six_months':
			defaultDateInterval = "month";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-6 months',time()))?>"});
			break;
		case 'one_year':
			defaultDateInterval = "year";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-1 year',time()))?>"});
			break;
		case 'two_years':
			defaultDateInterval = "year";
			TabState.setParam({"dateEnd":"<?=date('Y-m-d')?>","dateBegin":"<?=date('Y-m-d',strtotime('-2 years',time()))?>"});
			break;
		case 'all_results':
			// disable forward and backward buttons
			dojo.byId("forwardButton").disabled = true;
			dojo.byId("backButton").disabled = true;
			TabState.setParam({"dateEnd":"*"});
			break;
	}
	if (rowId != 'date_range') {
		TabState.redrawTab();
	}
}

loGrid.setImagePath("<?=$this->baseUrl?>/img/");
loGrid.setHeader('<?=__('Orders')?>');
loGrid.setInitWidths("*");
loGrid.setColTypes("ro");
loGrid.setSkin("xp");

loGrid.init();
loGrid.load('<?=$this->baseUrl?>/lab-results.raw/lab-orders.json',"json");


function setLabResultLabel() {
	var rowId = lrepGrid.getSelectedRowId();
	if (rowId == null) {
		rowId = lrepGrid.getRowId(0);
	}
	if (rowId == null) {
		setTimeout("setLabResultLabel();",1000)
		return;
	}
	var labResultLabel = lrepGrid.cells(rowId,0).getValue();
	var dateEnd = TabState.getParam("dateEnd");
	if (dateEnd == '*') {
		labResultLabel += ": <?=__('All Tests')?>";
	}
	else {
		var dateBegin = TabState.getParam("dateBegin");
		labResultLabel += ": "+dateBegin+" - "+dateEnd;
	}
	dojo.byId("labResultLabel").innerHTML = labResultLabel;
}
// set the lab result labels
setLabResultLabel();

</script>
